{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from scipy.interpolate import make_interp_spline\n",
    "\n",
    "plt.rcParams[\"font.sans-serif\"]=['SimHei']\n",
    "plt.rcParams['axes.unicode_minus'] = False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "rg = re.compile(r'\\$\\$.*?\\$\\$', re.S)\n",
    "size_rg = re.compile(r'(-[^-]*-)', re.S)\n",
    "rank_rg = re.compile(r'(\\+.*?\\+)', re.S)\n",
    "time_para_rg = re.compile(r'\\`.*?\\`', re.S)\n",
    "time_serial_rg = re.compile(r'\\@.*?\\@', re.S)\n",
    "speedup_rg = re.compile(r'\\#.*?\\#', re.S)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_file (filepath):\n",
    "    print('读取文件 ' + filepath)\n",
    "    fp = open(filepath, mode='r', encoding='utf-8')\n",
    "    if fp == None:\n",
    "        print('读取文件错误')\n",
    "        exit(2)\n",
    "    return rg.findall(fp.read())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "def parse_data (data):\n",
    "    proc_list = [2, 4, 8, 12]\n",
    "    parse_res = []\n",
    "    for d_loop in data:\n",
    "        size = eval(size_rg.findall(d_loop)[0].strip('-'))\n",
    "        time_para = eval(time_para_rg.findall(d_loop)[0].strip('`'))\n",
    "        time_serial = eval(time_serial_rg.findall(d_loop)[0].strip('@'))\n",
    "        speedup = eval(speedup_rg.findall(d_loop)[0].strip('#'))\n",
    "        proc_num = eval(rank_rg.findall(d_loop)[0].strip('+'))\n",
    "        # efficiency = time_serial / (time_para * proc_num)\n",
    "        parse_res.append([size, time_para, time_serial, speedup, proc_num])\n",
    "    return parse_res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "def smooth_xy(lx, ly):\n",
    "    \"\"\"数据平滑处理\n",
    "    :param lx: x轴数据，数组\n",
    "    :param ly: y轴数据，数组\n",
    "    :return: 平滑后的x、y轴数据，数组 [slx, sly]\n",
    "    \"\"\"\n",
    "    x = np.array(lx)\n",
    "    y = np.array(ly)\n",
    "    x_smooth = np.linspace(x.min(), x.max(), 300)\n",
    "    y_smooth = make_interp_spline(x, y)(x_smooth)\n",
    "    return [x_smooth, y_smooth]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "def proc_speedup(data_frame, save_name):\n",
    "    data = data_frame[['speedup', 'proc_num']]\n",
    "    data = data.groupby('proc_num').mean()['speedup']\n",
    "    index, values= smooth_xy(data.index, data.values)\n",
    "    ax=plt.axes()\n",
    "    plt.plot(index, values)\n",
    "    ax.set_xlabel('The number of parallel processes', fontsize=11, color='k')\n",
    "    ax.set_ylabel('speedup mean', fontsize=11, color='k')\n",
    "    ax.set_title('进程数和加速比的变化趋势', fontsize=11, color='k')\n",
    "    # plt.show()\n",
    "    plt.savefig(\"./image_result/proc_speedup_\"+save_name+'.png')\n",
    "    plt.clf() \n",
    "# data.plot(xlabel='The number of parallel processes', ylabel='speedup mean')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "def size_speedup(data_frame, save_name):\n",
    "    data2 = data_frame[['speedup', 'size']]\n",
    "    data2 = data2.groupby('size').mean()['speedup']\n",
    "    # data\n",
    "    # data.plot(xlabel='data size', ylabel='speedup mean', title='')\n",
    "    index, values= smooth_xy(data2.index, data2.values)\n",
    "    ax=plt.axes()\n",
    "    plt.plot(index, values)\n",
    "    ax.set_xlabel('The number of parallel processes', fontsize=11, color='k')\n",
    "    ax.set_ylabel('speedup mean', fontsize=11, color='k')\n",
    "    ax.set_title('数据规模和加速比的变化趋势', fontsize=11, color='k')\n",
    "    # plt.show()\n",
    "    plt.savefig(\"./image_result/size_speedup_\"+save_name+'.png')\n",
    "    plt.clf() "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {},
   "outputs": [],
   "source": [
    "def efficiency(efficiency_dataList):\n",
    "    time_size = []\n",
    "    max_lines = 11\n",
    "    for dl in efficiency_dataList:\n",
    "        data = dl[['parallel', 'size']]\n",
    "        data = data.groupby('size').mean()['parallel']\n",
    "        time_size.append([data.index, data.values])\n",
    "    plt.plot(time_size[0][0][:max_lines], time_size[0][1][:max_lines], label='mpi_bubble')\n",
    "    plt.plot(time_size[1][0][:19], time_size[1][1][:19], label='mpi_select')\n",
    "    plt.plot(time_size[2][0][:max_lines], time_size[2][1][:max_lines], label='mpi_quick')\n",
    "    plt.plot(time_size[3][0][:max_lines], time_size[3][1][:max_lines], label='opm_bubble')\n",
    "    plt.plot(time_size[4][0][:max_lines], time_size[4][1][:max_lines], label='opm_select')\n",
    "    plt.plot(time_size[5][0][:max_lines], time_size[5][1][:max_lines], label='omp_quick')\n",
    "    # ax=plt.axes()\n",
    "    # ax.set_xlabel('Participate in the operation of data size', fontsize=11, color='k')\n",
    "    # ax.set_ylabel('Time-consuming elapsed time', fontsize=11, color='k')\n",
    "    # ax.set_title('Time-DataSize', fontsize=11, color='k')\n",
    "    plt.legend()\n",
    "    # plt.show()\n",
    "    plt.savefig(\"./image_result/Time-DataSize.png\")\n",
    "    plt.clf() \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "读取文件 result_mpi_bubble.txt\n",
      "获取数据成功，数据如下, 即将开始数据格式化\n",
      "数据解析开始\n",
      "数据解析完成，已保存数据到excel和image结果文件夹！\n",
      "读取文件 result_select_mpi.txt\n",
      "获取数据成功，数据如下, 即将开始数据格式化\n",
      "数据解析开始\n",
      "数据解析完成，已保存数据到excel和image结果文件夹！\n",
      "读取文件 result_quick_mpi.txt\n",
      "获取数据成功，数据如下, 即将开始数据格式化\n",
      "数据解析开始\n",
      "数据解析完成，已保存数据到excel和image结果文件夹！\n",
      "读取文件 result_bubble_opm.txt\n",
      "获取数据成功，数据如下, 即将开始数据格式化\n",
      "数据解析开始\n",
      "数据解析完成，已保存数据到excel和image结果文件夹！\n",
      "读取文件 result_select_opm.txt\n",
      "获取数据成功，数据如下, 即将开始数据格式化\n",
      "数据解析开始\n",
      "数据解析完成，已保存数据到excel和image结果文件夹！\n",
      "读取文件 result_quick_omp.txt\n",
      "获取数据成功，数据如下, 即将开始数据格式化\n",
      "数据解析开始\n",
      "数据解析完成，已保存数据到excel和image结果文件夹！\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<Figure size 640x480 with 0 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "filepath_list = ['result_mpi_bubble.txt', 'result_select_mpi.txt', 'result_quick_mpi.txt',\n",
    "    'result_bubble_opm.txt', 'result_select_opm.txt', 'result_quick_omp.txt']\n",
    "efficiency_dataList = []\n",
    "for fl in filepath_list:\n",
    "    data_list = parse_data(get_file(fl))\n",
    "    print('获取数据成功，数据如下, 即将开始数据格式化')\n",
    "    data_frame = pd.DataFrame(data_list, columns=['size', 'parallel', 'serial', 'speedup', 'proc_num'])\n",
    "    efficiency_dataList.append(data_frame)\n",
    "    print('数据解析开始')\n",
    "    size_speedup(data_frame, fl.split('.')[0])\n",
    "    proc_speedup(data_frame, fl.split('.')[0])\n",
    "    data_frame.to_excel(\"./excel_result/\"+ fl.split('.')[0] + \".xlsx\", index=False)\n",
    "    print('数据解析完成，已保存数据到excel和image结果文件夹！')\n",
    "efficiency(efficiency_dataList)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "# ! python -m pip install --upgrade pip\n",
    "# ! pip install openpyxl -i https://pypi.douban.com/simple/"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "# data = data_frame[['speedup', 'size']]\n",
    "# data = data.groupby('size').mean()['speedup']\n",
    "# index, values= smooth_xy(data.index, data.values)\n",
    "# plt.plot(values, index, label='size', color='r')\n",
    "# plt.legend(loc=\"upper left\")\n",
    "# data = data_frame[['speedup', 'proc_num']]\n",
    "# data = data.groupby('proc_num').mean()['speedup']\n",
    "# index, values= smooth_xy(data.index, data.values)\n",
    "# ax2 = plt.twinx()\n",
    "# ax2.plot(values, index, label='proc num', color='g')\n",
    "# plt.legend()\n",
    "# plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "# import matplotlib.pyplot as plt\n",
    "\n",
    "# x = [1, 2, 3, 4, 5]  # x轴\n",
    "# # 第一条折线\n",
    "# y1 = [0.2, 0.4, 0.6, 0.8, 1.0]  # y轴1\n",
    "# # 第二条折线\n",
    "# y2 = [2, 4, 6, 8, 10]  # y轴2\n",
    "# # 设置折线图的宽度为6\n",
    "# plt.figure(figsize=(6, 4))\n",
    "# # 绘制第一条折线\n",
    "# plt.plot(x, y1, label=\"line 1\", color='r')\n",
    "# # 绘制第二条折线\n",
    "# plt.plot(x, y2, label=\"line 2\", color='g')\n",
    "# # 对第二条折线设置另一个纵坐标\n",
    "# ax2 = plt.twinx()\n",
    "# ax2.plot(x, y2, label=\"line 2\", color='g')\n",
    "# # 设置图例的位置\n",
    "# plt.legend(loc=\"upper left\")\n",
    "# # 显示图像\n",
    "# plt.show()\n",
    "# # fig.savefig(\"./image_result/first.png\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.9"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "667d39e1ca76f9985cd03c132b561dc9823709af0a3754766fb916fa5f7fff16"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
